package com.ssm.service.impl;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import com.ssm.mapper.CardMapper;
import com.ssm.service.CardService;
import com.sun.jna.Library;
import com.sun.jna.Memory;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.springframework.beans.factory.annotation.Autowired;

import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceException;
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@org.springframework.stereotype.Service
public class CardServiceImpl implements CardService {
    @Autowired
    CardMapper cardMapper;
    Call call;
    //管理员用户名
    String userName="411300";
//    管理员密码
    String passWord="411300vTZd18148h";
//    用户身份证号
String CardNum="";
    //    用户姓名
    String CardName="";
//    城市代码
    String postalCode;
//    社保卡号
    String cardNumber;
//    社保卡号有效期
    String cardDataTime;
//    性别
    String cardSex;
//    民族
    String cardNation;
//    出生日期
    String cardData;
//    人员状态
    String cardStatus;
//    户口性质
    String cardType;
//    户口所在地址
    String cardAddress;
//    联系手机
    String cardPhone;
//    联系电话
    String cardTel;
//    邮政编码
    String cardPost;
//    邮政地址
    String cardPostAddress;
//    个人编号
    String cardPeopleNo;
//    单位编号
    String cardWorkNo;
//    单位名称
    String cardWorkName;
//    监护人姓名
    String cardParentName;
//    监护人证号
    String cardParentNum;
//    监护人证件类型
    String cardNumType;
//    操作人员机构编号
    String cardUserNo;
//    操作人员用户名称
    String cardUserName;
//    操作日志
    String cardUserData;
//    有效标志
    String cardMark;



    {
        try {
            call = (Call) (new Service()).createCall();
            call.setTargetEndpointAddress(new URL("http://10.120.1.18:7001/hnCardService/services/CardService"));
//
//            call.setTimeout(30000);
        } catch (ServiceException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }
    public interface HDReadCardzx extends Library {

        HDReadCardzx INSTANCE = (HDReadCardzx) Native.loadLibrary("HDReadCardzx", HDReadCardzx.class);//加载动态库文件
        //动态库中调用的方法
        int ReadCertInfo(String pBmpFile, Pointer pName, Pointer pSex, Pointer pNation, Pointer pBirth, Pointer pAddress, Pointer pCertNo, Pointer pDepartment
                , Pointer pEffectData, Pointer pExpire, Pointer pErrMsg
        );
    }
  public void voise(String vois){
      ActiveXComponent sap = new ActiveXComponent("Sapi.SpVoice");
      Dispatch sapo = sap.getObject();
      try {
          // 音量 0-100  
          sap.setProperty("Volume", new Variant(100));
          // 语音朗读速度 -10 到 +10  
          sap.setProperty("Rate", new Variant(0));
          // 执行朗读  
          Dispatch.call(sapo, "Speak", new Variant(vois));
      } catch (Exception e) {
          e.printStackTrace();
      } finally {
          sapo.safeRelease();
          sap.safeRelease();
      }
  }
  public static   Map rescard(){
      System.setProperty("jna.encoding", "GBK");
      String pBmpFile = "C:\\Users\\MAIBENBEN\\IdeaProjects\\card_client\\src\\main\\webapp\\imgs\\zb.bmp";
      Pointer pName = new Memory(10);
      Pointer pSex = new Memory(2);
      Pointer pNation = new Memory(2);
      Pointer pBirth = new Memory(50);
      Pointer pAddress = new Memory(40);
      Pointer pCertNo = new Memory(18);
      Pointer pDepartment = new Memory(50);
      Pointer pEffectData = new Memory(50);
      Pointer pExpire = new Memory(50);
      Pointer pErrMsg = new Memory(50);
      System.out.println(pName);
//        System.out.println(System.getProperty("java.library.path"));
      int card = HDReadCardzx.INSTANCE.ReadCertInfo(pBmpFile, pName, pSex, pNation, pBirth, pAddress, pCertNo, pDepartment, pEffectData, pExpire, pErrMsg);
      Map map = new HashMap();
     map.put("pName", new String(pName.getByteArray(0, 10)));
//      map.put("pSex", new String(pSex.getByteArray(0, 2)));
//      map.put("pNation", new String(pNation.getByteArray(0, 2)));
      map.put("result",card);
//      map.put("pBirth", new String(pBirth.getByteArray(0, 50)));
      map.put("pAddress", new String(pAddress.getByteArray(0, 40)));
      map.put("pCertNo", new String(pCertNo.getByteArray(0, 18)));
      System.out.println(map);
//      map.put("pDepartment", new String(pDepartment.getByteArray(0, 50)));
//      map.put("pEffectData", new String(pEffectData.getByteArray(0, 50)));
//      map.put("pExpire", new String(pExpire.getByteArray(0, 50)));
//      map.put("pErrMsg", new String(pErrMsg.getByteArray(0, 50)));
//                System.out.print((char) pName.getByteArray(0, 50)[i]);
      return map;
  }



    //    调用getCard（）方法进行卡状态的查询，如果卡状态是未领取启用，需要先来领取启用
    @Override
    public String CardStatus() throws Exception {
//        call.setOperationName(new QName("http://ws.apache.org/axis2","getAC01"));
//        call.setTimeout(20000);
//        String str= (String) call.invoke(new String[]{userName,passWord,CardNum,CardName});
//        System.out.println("查询出的身份证信息是："+str);
//        Pattern compile = Pattern.compile("<AAZ500>(.*)</AAZ500>");
//        Matcher matcher = compile.matcher(str);
//        while (matcher.find()){
//            cardNumber=matcher.group(1);
//            System.out.println("cardNumber的值是："+cardNumber);
//        }
        call.setOperationName(new QName("http://ws.apache.org/axis2","getCard"));
        call.setTimeout(10000);
        String str2= (String) call.invoke(new String[]{userName,passWord,cardNumber,CardNum,CardName});
        System.out.println("str的值是："+str2);
        return str2;
    }
    //    调用getAC01()方法来进行卡数据的查询
    @Override
    public String CardDataQuery() throws Exception {
        voise("请将身份证放入身份证阅读区");
        call.setOperationName(new QName("http://ws.apache.org/axis2","getAC01"));
        call.setTimeout(30000);
        Map map=rescard();
        CardNum=map.get("pCertNo").toString().trim();
        CardName=map.get("pName").toString().trim();
        System.out.println(map+"===="+CardName);
        String str1= (String) call.invoke(new String[]{userName,passWord,CardNum,CardName});
        System.out.println("查询出的身份证信息是："+str1);
        Pattern compile1 = Pattern.compile("<AAZ500>(.*)</AAZ500>");
        Pattern compile2 = Pattern.compile("<ZJYXQ>(.*)</ZJYXQ>");
        Pattern compile3 = Pattern.compile("<ZJYXQ>(.*)</ZJYXQ>");
        Matcher matcher1 = compile1.matcher(str1);
        Matcher matcher2 = compile2.matcher(str1);
        while (matcher1.find()&&matcher2.find()){
            cardNumber=matcher1.group(1);
            cardDataTime=matcher2.group(1);
            System.out.println("cardNumber的值是："+cardNumber+"cardDataTime的值是："+cardDataTime);
        }
//        JSONObject jsonObject = XML.toJSONObject(str);
//        System.out.println(jsonObject);
        return str1;
    }
//银行数据查询
    @Override
    public  String CardDataBank() throws RemoteException {
        call.setOperationName(new QName("http://ws.apache.org/axis2","getBank"));
        call.setTimeout(30000);
        String str1= (String) call.invoke(new String[]{userName,passWord,CardNum,CardName});
        return str1;
    }

    @Override
    public String CardPeopleData() throws RemoteException {
        call.setOperationName(new QName("http://ws.apache.org/axis2","getRysj"));
        String str1= (String) call.invoke(new String[]{userName,passWord,CardNum,CardName});
        return str1;
    }

    @Override
    public String CardPhoneData() throws RemoteException {
        call.setOperationName(new QName("http://ws.apache.org/axis2","getPhoto"));
        String str1= (String) call.invoke(new String[]{userName,passWord,CardNum,CardName});
        return str1;
    }

    //    如果卡未激活，调用allDsjk()方法进行领卡启用
    @Override
    public Object CardCollar() throws RemoteException {
//        String str="<操作*>领卡启用</操作*><用户名*>411300</用户名*><密码*>411300vTZd18148h</密码*><城市代码*>411600</城市代码*><社会保障卡卡号*>P86502299</社会保障卡卡号*><社会保障号码*>412726199609120821</社会保障号码*><姓名*>位路路</姓名*>";
        String str="<操作*>领卡启用</操作*><用户名*>"+userName+"</用户名*><密码*>"+passWord+"</密码*><城市代码*>"+postalCode+"</城市代码*><社会保障卡卡号*>"+cardNumber+"</社会保障卡卡号*><社会保障号码*>"+CardNum+"</社会保障号码*><姓名*>"+CardName+"</姓名*>";
        call.setOperationName(new QName("http://ws.apache.org/axis2","allDsjk"));
//        call.setUseSOAPAction(true);
//        call.addParameter("xmlBoby", XMLType.XSD_STRING, ParameterMode.IN);
//        call.setReturnClass(String.class);
//        Object object="";
        Object object=call.invoke(new Object[]{str});
        return object;
    }

    @Override
    public String CardData() throws RemoteException {
        call.setOperationName(new QName("http://ws.apache.org/axis2","getData"));
        String str1= (String) call.invoke(new String[]{userName,passWord,"411329198708110030","王梁奎"});
        return str1;
    }

    //    调用allDsjk()方法来正式挂失卡
    @Override
    public Object OfficialLossCard() throws RemoteException {
        String str="<操作*>正式挂失</操作*><用户名*>"+userName+"</用户名*><密码*>"+passWord+"</密码*><城市代码*>"+postalCode+"</城市代码*><社会保障卡卡号*>"+cardNumber+"</社会保障卡卡号*><社会保障号码*>"+CardNum+"</社会保障号码*><姓名*>"+CardName+"</姓名*>";
        call.setOperationName(new QName("http://ws.apache.org/axis2","allDsjk"));
//        call.setUseSOAPAction(true);
//        call.addParameter("xmlBoby", XMLType.XSD_STRING, ParameterMode.IN);
//        call.setReturnClass(String.class);
        Object object="";
        object=call.invoke(new Object[]{str});
        return object;
    }
    //    补全卡信息（申请社保卡）
    @Override
    public Object CompleteCardInformation(String phone) throws RemoteException {
        System.out.println("phone的值是："+phone);
        String str2="<操作*>申请社保卡</操作*><用户名*>"+userName+"</用户名*><密码*>"+passWord+"</密码*><城市代码*>"+postalCode+"</城市代码*><经办机构*>"
                +postalCode+"00<人员类别*>1</人员类别*><服务银行*>95566</服务银行*><国籍*>CHN</国籍*><证件类型*>1</证件类型*><证件号码*>"+CardNum+"</证件号码*>"
                +"<证件有效期*>"+cardDataTime+"</证件有效期*><姓名*>"+CardName+"</姓名*>" +"<移动电话>"+phone+"</移动电话>"
                +"<办卡类型*>5</办卡类型*><补换原因>5</补换原因><社保卡号>"+cardNumber+"</社保卡号>";
        call.setOperationName(new QName("http://ws.apache.org/axis2","allDsjk"));
//        call.setUseSOAPAction(true);
//        call.addParameter("xmlBoby", XMLType.XSD_STRING, ParameterMode.IN);
//        call.setReturnClass(String.class);
        Object ob=call.invoke(new Object[]{str2});
        return ob;
    }

    @Override
    public int insertCard(String pName, String pSex, String pSocialNumber, String pIDCard) {
        System.out.println("测试这一层的数据"+"pName"+pName+"pSex"+pSex+"pSocialNumber"+pSocialNumber);
        return cardMapper.insertCard(pName,pSex,pSocialNumber,pIDCard);
    }
}
